﻿using Dm.filter.log;
using Newtonsoft.Json;
using PLM.ConfigurationCenter.Core;
using PLM.ConfigurationCenter.Core.Expansion;
using PLM.ConfigurationCenter.CustomControl;
using PLM.ConfigurationCenter.CustomForm;
using PLM.ConfigurationCenter.EventBus.RabbitMQ;
using PLM.ConfigurationCenter.Handler;
using PLM.ConfigurationCenter.Jobs;
using PLM.ConfigurationCenter.Model;
using PLM.ConfigurationCenter.Serilog;
using PLM.ConfigurationCenter.Service;
using PLM.ConfigurationCenter.SqlSugar;
using PLM.Service.CommomItem.Log.Dto;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace PLM.ConfigurationCenter {
    public partial class MainForm : UIForm {
        /// <summary>
        /// 
        /// </summary>
        public ServerService _server = new ServerService();

        public DbSyncForm checkConfigForm;

        private IRabbitMQConnection _mQConnection;
        /// <summary>
        /// 
        /// </summary>
        private LogHandler logHandler;
        /// <summary>
        /// 
        /// </summary>
        private LogInfoService _logInfoService;
        /// <summary>
        /// 
        /// </summary>
        private List<SystemLogModel> systemLogs = new();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mQConnection"></param>
        public MainForm(IRabbitMQConnection mQConnection, LogInfoService infoService) {
            InitializeData();
            InitializeComponent();
            logHandler = new LogHandler(this.tb_loginfo);

            LogInfoHelper.WriteLog("sdasda");
            _mQConnection = mQConnection;
            _logInfoService = infoService;
        }




        /// <summary>
        /// 
        /// </summary>
        public void InitCheck() {
            var rootDic = DbManager.DbContext.Queryable<SystemSettingModel>().First(p => p.Name == "RootDic");
            if (rootDic == null || rootDic.Value.IsEmpty()) {
                logHandler.WriteLog("错误", $"主文件夹未配置！");
                return;
            }
            if (!Directory.Exists(rootDic.Value)) {
                logHandler.WriteLog("错误", $"【{rootDic.Value}】主文件夹未找到！");
                return;
            }
            var model = ServerHandler.GetServerModel("API");
            ServerCardClick(model);
            CheckServer();
            AutoStart();
        }

        /// <summary>
        /// 
        /// </summary>
        private static async void InitializeData() {
            DbManager.DbContext.CodeFirst.InitTables<ServerModel>();
            DbManager.DbContext.CodeFirst.InitTables<SystemSettingModel>();
            DbManager.DbContext.CodeFirst.InitTables<DatabaseInfoModel>();
            DbManager.DbContext.CodeFirst.InitTables<LogInfoModel>();

            var list = await DbManager.DbContext.Queryable<ServerModel>().ToListAsync();
            if (!list.Any()) {
                list = ServerHandler.GetDefaultServerList();
                await DbManager.DbContext.Insertable(list).ExecuteCommandAsync();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private async void InitializeServerCard() {
            this.panel_serverlist.Controls.Clear();
            var list = await DbManager.DbContext.Queryable<ServerModel>().ToListAsync();
            int height = 88;
            for (int i = 0; i < list.Count; i++) {
                int temp = (height * i);
                ServerCard crad = new();
                crad.Init(list[i]);
                crad.Name = $"crad_{list[i].Id}";
                crad.CradClick += ServerCardClick;
                crad.CradDoubleClick += ServerCardDoubleClick;
                crad.Location = new Point(22, temp);
                this.panel_serverlist.Controls.Add(crad);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        private void ServerCardClick(ServerModel model) {
            if (!CheckRootDic()) {
                return;
            }
            this.lb_serverstatus.Text = ServerHandler.GetSiteStatusInfo(model);
            foreach (ServerCard item in this.panel_serverlist.Controls) {
                if ($"crad_{model.Id}" == item.Name) {
                    item.uIPanel.FillColor = Color.FromArgb(54, 133, 254);
                } else {
                    item.uIPanel.FillColor = Color.FromArgb(27, 42, 75);
                }
                Changelight(model);
            }
            this.lb_serverid.Text = model.Id;
            this.lb_servername.Text = model.SiteName;
            this.lb_serverport.Text = model.SitePort.ToString();
            this.lb_serverip.Text = model.IpAddress;
            this.lb_netadress.Text = $"{this.lb_serverip.Text}:{this.lb_serverport.Text}";
            if (!model.SiteName.IsEmpty()) {
                this.lb_serverpath.Text = ServerHandler.GetSitePath(model.Id, model.SiteName);
            }
            CheckButtonStatus(model);
        }
        /// <summary>
        /// 
        /// </summary>
        private void ServerCardDoubleClick(ServerModel model) {
            if (model.Status >= 3) {
                MessageBox.Show("服务已安装不能修改,请先卸载后再进行修改！", "提示");
                return;
            }
            ServerSettingForm form = new ServerSettingForm(model);
            form.StartPosition = FormStartPosition.CenterParent;
            if (form.ShowDialog() == DialogResult.OK) {
                var res = ServerHandler.GetServerModel(model.Id);
                ServerCardClick(res);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_setting_Click(object sender, System.EventArgs e) {
            SystemSettingForm settingForm = new() {
                StartPosition = FormStartPosition.CenterParent
            };
            if (settingForm.ShowDialog() == DialogResult.OK) {
                InitCheck();
            };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_maindb_Click(object sender, System.EventArgs e) {
            MainDbForm settingForm = new MainDbForm("BaseDb");
            settingForm.StartPosition = FormStartPosition.CenterParent;
            //settingForm.RefreshClick += Init;
            settingForm.ShowDialog();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_auxiliarydb_Click(object sender, System.EventArgs e) {
            MainDbForm settingForm = new MainDbForm("AuxiliaryDb");
            settingForm.StartPosition = FormStartPosition.CenterParent;
            //settingForm.RefreshClick += Init;
            settingForm.ShowDialog();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_CheckConfig_Click(object sender, EventArgs e) {
            DbSyncForm checkConfigForm = AppHelper.GetService<DbSyncForm>();
            checkConfigForm.StartPosition = FormStartPosition.CenterParent;
            checkConfigForm.ShowDialog();
        }
        /// <summary>
        /// 
        /// </summary>
        private bool CheckRootDic() {
            var rootDic = DbManager.DbContext.Queryable<SystemSettingModel>().First(p => p.Name == "RootDic");
            if (rootDic == null) {
                UIMessageDialog.ShowMessageDialog("主文件夹未设置！请先设置主文件夹", UILocalize.InfoTitle, false, Style);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, System.EventArgs e) {
            this.lb_time.Text = $"当前时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}";
        }

        /// <summary>
        /// 
        /// </summary>
        private void CheckButtonStatus(ServerModel model) {
            switch (model.Status) {
                case 1:
                    this.btn_init.Enabled = true;
                    this.btn_install.Enabled = false;
                    this.btn_uninstall.Enabled = false;
                    this.btn_start.Enabled = false;
                    this.btn_stop.Enabled = false;
                    this.btn_update.Enabled = false;
                    break;
                case 2:
                    this.btn_init.Enabled = true;
                    this.btn_install.Enabled = true;
                    this.btn_uninstall.Enabled = false;
                    this.btn_start.Enabled = false;
                    this.btn_stop.Enabled = false;
                    this.btn_update.Enabled = false;
                    break;
                case 3:
                    this.btn_init.Enabled = false;
                    this.btn_install.Enabled = false;
                    this.btn_uninstall.Enabled = true;
                    this.btn_start.Enabled = true;
                    this.btn_stop.Enabled = false;
                    this.btn_update.Enabled = true;
                    break;
                case 4:
                    this.btn_init.Enabled = false;
                    this.btn_install.Enabled = false;
                    this.btn_uninstall.Enabled = false;
                    this.btn_start.Enabled = false;
                    this.btn_stop.Enabled = true;
                    this.btn_update.Enabled = false;
                    break;
                default:
                    this.btn_init.Enabled = false;
                    this.btn_install.Enabled = false;
                    this.btn_uninstall.Enabled = false;
                    this.btn_start.Enabled = false;
                    this.btn_stop.Enabled = false;
                    this.btn_update.Enabled = false;
                    break;
            }
            if (model.Id.ToLower() == "redis") {
                this.btn_update.Visible = false;
            } else {
                this.btn_update.Visible = true;
            }
        }
        #region 检查服务
        /// <summary>
        /// 
        /// </summary>
        private void CheckServer() {
            logHandler.WriteLog("信息", "开始检查服务...");
            var list = _server.GetServerModels();
            foreach (ServerModel item in list) {

                if (item.SiteName.IsEmpty()) {
                    logHandler.WriteLog("警告", $"{item.SiteLabel} 未配置,已跳过检查!");
                } else {
                    ServerModel res = item.Id switch {
                        "REDIS" => ServerHandler.RedisCheck(item, logHandler),
                        "API" => ServerHandler.IISCheck(item, logHandler),
                        "WEB" => ServerHandler.NginxCheck(item, logHandler),
                        "COMMONITEM" => ServerHandler.ServiceCheck("PLM.WebApi.CommonItem", item, logHandler),
                        _ => item,
                    };
                    Changelight(res);
                }

            }
            logHandler.WriteLog("信息", "服务检查完成");
        }
        #endregion
        #region 操作按钮
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_init_Click(object sender, System.EventArgs e) {
            if (!CheckRootDic()) {
                return;
            }
            if (this.lb_servername.Text.IsNullOrEmpty()) {
                MessageBox.Show("请先配置服务信息！");
                return;
            }
            ShowWaitForm();
            var model = DbManager.DbContext.Queryable<ServerModel>().First(p => p.Id == this.lb_serverid.Text);
            logHandler.WriteLog("信息", $"初始化【{model.SiteLabel}】配置文件！");
            var sitePath = ServerHandler.GetSitePath(model.Id, model.SiteName);
            bool res = false;
            string error = "";
            switch (model.Id) {
                case "WEB":
                    res = ServerHandler.InitWebConfig(out error);
                    break;
                case "COMMONITEM":
                    res = ServerHandler.InitCommonitemConfig(sitePath, out error);
                    break;
                case "FILESTORE":
                    res = ServerHandler.InitFileStoreConfig(sitePath, out error);
                    break;
                case "REDIS":
                    res = ServerHandler.InitRedis(sitePath, out error);
                    break;
                case "API":
                    res = ServerHandler.InitApiConfig(out error);
                    break;
                default:
                    break;
            }
            HideWaitForm();
            if (res) {
                logHandler.WriteLog("信息", $"【{model.SiteLabel}】初始化配置文件成功！");
                MessageBox.Show("初始化配置完成");
                model.Status = 2;
                DbManager.DbContext.Updateable(model).ExecuteCommand();
                ServerCardClick(model);
            } else {
                logHandler.WriteLog("错误", $"【{model.SiteLabel}】初始化配置文件失败！{error}");
                MessageBox.Show("初始化配置失败！" + error);
            }
        }
        /// <summary>
        /// 服务安装
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_install_Click(object sender, EventArgs e) {
            bool state = false;
            string error = "";
            ShowWaitForm();
            var CardModel = DbManager.DbContext.Queryable<ServerModel>().First(p => p.Id == this.lb_serverid.Text);
            logHandler.WriteLog("信息", $"安装【{CardModel.SiteLabel}】！");
            switch (CardModel.Id) {
                case "REDIS":
                    state = ServerHandler.RedisServiceInstall(CardModel, out error);
                    break;
                case "WEB":
                    state = ServerHandler.NginxInstall(CardModel, out error);
                    break;
                case "API":
                    state = ServerHandler.IISServiceInstall(CardModel, out error);
                    break;
                case "COMMONITEM":
                    state = ServerHandler.CommonServiceInstall(CardModel, "PLM.WebApi.CommonItem", out error);
                    break;
                case "FILESTORE":
                    state = ServerHandler.CommonServiceInstall(CardModel, "PLM.Web.FileStore", out error);
                    break;
                case "ADAPTER":
                    state = ServerHandler.CommonServiceInstall(CardModel, "PLM.Web.Adapter", out error);
                    break;
                case "JOBCENTER":
                    state = ServerHandler.CommonServiceInstall(CardModel, "PLM.Web.JobCenter", out error);
                    break;
                default:
                    break;
            }
            HideWaitForm();
            if (state) {
                logHandler.WriteLog("信息", $"【{CardModel.SiteLabel}】安装成功！");
                CardModel.Status = 3;
                DbManager.DbContext.Updateable(CardModel).ExecuteCommand();
                ServerCardClick(CardModel);
                MessageBox.Show("安装完成");
            } else {
                logHandler.WriteLog("错误", $"【{CardModel.SiteLabel}】安装失败！{error}");
                MessageBox.Show("安装失败！" + error);
            }
        }
        /// <summary>
        /// 服务卸载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_uninstall_Click(object sender, EventArgs e) {
            bool state = false;
            string error = "";
            ShowWaitForm();
            var CardModel = DbManager.DbContext.Queryable<ServerModel>().First(p => p.Id == this.lb_serverid.Text);
            logHandler.WriteLog("信息", $"卸载【{CardModel.SiteLabel}】！");
            switch (CardModel.Id) {
                case "REDIS":
                    state = ServerHandler.RedisServiceUninstall(CardModel, out error);
                    break;
                case "WEB":
                    state = ServerHandler.NginxUninstall(CardModel, out error);
                    break;
                case "API":
                    state = ServerHandler.IISServiceUninstall(CardModel, out error);
                    break;
                case "COMMONITEM":
                case "FILESTORE":
                case "ADAPTER":
                case "JOBCENTER":
                    state = ServerHandler.CommonServiceUninstall(CardModel, out error);
                    break;
                default:
                    break;
            }
            HideWaitForm();
            if (state) {
                logHandler.WriteLog("信息", $"【{CardModel.SiteLabel}】卸载成功！");
                CardModel.Status = 2;
                DbManager.DbContext.Updateable(CardModel).ExecuteCommand();
                ServerCardClick(CardModel);
                MessageBox.Show("卸载成功");
            } else {
                logHandler.WriteLog("错误", $"【{CardModel.SiteLabel}】卸载失败！{error}");
                MessageBox.Show("卸载失败！" + error);
            }
        }
        /// <summary>
        /// 服务启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_start_Click(object sender, EventArgs e) {
            var CardModel = DbManager.DbContext.Queryable<ServerModel>().First(p => p.Id == this.lb_serverid.Text);
            logHandler.WriteLog("信息", $"启动【{CardModel.SiteLabel}】！");
            //检查端口是否被占用
            bool state = ServerHandler.CheckSitePort(CardModel.SitePort);
            if (state) {
                var str = $"【{CardModel.SiteLabel}】端口:{CardModel.SitePort} 已被占用";
                logHandler.WriteLog("错误", $"启动【{CardModel.SiteLabel}】失败！{str}");
                MessageBox.Show(str, "提示");
                return;
            }
            ShowWaitForm();
            state = StartServer(CardModel, out string error);
            HideWaitForm();
            if (state) {
                logHandler.WriteLog("信息", $"【{CardModel.SiteLabel}】启动成功！");
                CardModel.Status = 4;
                DbManager.DbContext.Updateable(CardModel).ExecuteCommand();
                ServerCardClick(CardModel);
                ShowSuccessTip("启动成功！");
            } else {
                logHandler.WriteLog("错误", $"【{CardModel.SiteLabel}】启动失败！{error}");
                UIMessageDialog.ShowMessageDialog("启动失败！", UILocalize.InfoTitle, false, Style);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private bool StartServer(ServerModel CardModel, out string error) {
            error = "";
            bool state = false;
            switch (CardModel.Id) {
                case "REDIS":
                    state = ServerHandler.ServiceStart(CardModel.SiteName.ToLower(), out error);
                    break;
                case "WEB":
                    state = ServerHandler.NginxSatrt(CardModel, out error);
                    break;
                case "API":
                    state = ServerHandler.StartSite(CardModel.SiteName, out error);
                    break;
                case "COMMONITEM":
                case "FILESTORE":
                case "ADAPTER":
                case "JOBCENTER":
                    state = ServerHandler.ServiceStart(CardModel.SiteName, out error);
                    break;
                default:
                    break;
            }
            return state;
        }
        /// <summary>
        /// 服务停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_stop_Click(object sender, EventArgs e) {
            ShowWaitForm();
            bool state = false;
            string error = "";
            var CardModel = DbManager.DbContext.Queryable<ServerModel>().First(p => p.Id == this.lb_serverid.Text);
            logHandler.WriteLog("信息", $"停止【{CardModel.SiteLabel}】！");
            state = CardModel.Id switch {
                "REDIS" => ServerHandler.ServiceStop(CardModel.SiteName.ToLower(), out error),
                "WEB" => ServerHandler.NginxStop(CardModel, out error),
                "API" => ServerHandler.StopSite(CardModel, logHandler),
                "COMMONITEM" or "FILESTORE" or "ADAPTER" or "JOBCENTER" => ServerHandler.ServiceStop(CardModel.SiteName, out error),
                _ => false,
            };
            HideWaitForm();
            if (state) {
                logHandler.WriteLog("信息", $"【{CardModel.SiteLabel}】停止成功！");
                CardModel.Status = 3;
                DbManager.DbContext.Updateable(CardModel).ExecuteCommand();
                ServerCardClick(CardModel);
                ShowSuccessTip("停止成功！");
            } else {
                logHandler.WriteLog("错误", $"【{CardModel.SiteLabel}】停止失败！{error}");
                UIMessageDialog.ShowMessageDialog("停止失败", UILocalize.InfoTitle, false, Style);
            }
        }
        /// <summary>
        /// 更新服务
        /// </summary>
        private void btn_update_Click(object sender, EventArgs e) {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件夹";
            dialog.Filter = "压缩文件(*.zip)|*.zip";
            if (dialog.ShowDialog() != DialogResult.OK) {
                return;
            }
            var CardModel = DbManager.DbContext.Queryable<ServerModel>().First(p => p.Id == this.lb_serverid.Text);
            logHandler.WriteLog("信息", $"更新【{CardModel.SiteLabel}】！");
            string file = dialog.FileName;
            var fileName = Path.GetFileNameWithoutExtension(file);
            if (fileName != CardModel.SiteName) {
                MessageBox.Show("更新文件包错误！");
                return;
            }
            ShowWaitForm();
            bool state = ServerHandler.BackupSite(CardModel, out string error);
            HideWaitForm();
            if (!state) {
                logHandler.WriteLog("错误", $"【{CardModel.SiteLabel}】备份失败！{error}");
                MessageBox.Show("备份文件失败！" + error);
                return;
            }
            logHandler.WriteLog("信息", $"【{CardModel.SiteLabel}】备份成功！");

            ShowWaitForm();
            state = ServerHandler.UpdateSite(CardModel, file, out error);
            HideWaitForm();
            if (state) {
                logHandler.WriteLog("信息", $"【{CardModel.SiteLabel}】更新成功！");
                MessageBox.Show("更新文件成功");
            } else {
                logHandler.WriteLog("错误", $"【{CardModel.SiteLabel}】更新失败！{error}");
                MessageBox.Show("更新文件失败！" + error);
            }
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void AutoStart() {
            var autoStart = DbManager.DbContext.Queryable<SystemSettingModel>().Where(p => p.Name == "AutoStart").First();
            QuartzHelper quartz = new();
            if (autoStart != null && autoStart.Value.ToBoolean()) {
                var groups = quartz.GetJobGroupNames();
                if (!groups.Contains("AutoStart_Group")) {
                    quartz.ScheduleJob<AutoStartJob>($"AutoStart_Job", "AutoStart_Group", $"AutoStart_Trigger", $"AutoStart_TriggerGroup", "0 0/10 * * * ?", ServerAutoStart);
                    await quartz.Start();
                    logHandler.WriteLog("信息", "服务检测启动成功！...");
                }
            } else {
                await quartz.Stop();
                logHandler.WriteLog("信息", "服务检测已关闭...");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void ServerAutoStart() {
            var list = DbManager.DbContext.Queryable<ServerModel>().ToList();
            foreach (var item in list) {
                if (item.Status < 3) {
                    logHandler.WriteLog("警告", $"【{item.SiteLabel}】服务未部署！");
                    continue;
                }
                ServerModel res = item.Id switch {
                    "REDIS" => ServerHandler.RedisAutoStart(item, logHandler),
                    "API" => ServerHandler.IISAutoStart(item, logHandler),
                    "WEB" => ServerHandler.NginxAutoStart(item, logHandler),
                    "COMMONITEM" or "FILESTORE" or "ADAPTER" or "JOBCENTER" => ServerHandler.ServiceAutoStart(item, logHandler),
                    _ => item,
                };
                if (item.Id == this.lb_serverid.Text.Trim()) {
                    ServerCardClick(res);
                }
                Changelight(res);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        private void Changelight(ServerModel model) {
            foreach (ServerCard item in this.panel_serverlist.Controls) {
                if ($"crad_{model.Id}" == item.Name) {
                    if (model.Status == 3) {
                        item.uiLight.BeginInvoke(new Action(() => {
                            item.uiLight.State = UILightState.On;
                        }));
                    } else if (model.Status == 4) {
                        item.uiLight.BeginInvoke(new Action(() => {
                            item.uiLight.State = UILightState.Blink;
                        }));
                    } else {
                        item.uiLight.BeginInvoke(new Action(() => { item.uiLight.State = UILightState.Off; }));
                    }
                }
            }
        }
        #region 窗口事件

        /// <summary>
        /// 窗口退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void StartForm_FormClosing(object sender, FormClosingEventArgs e) {
            if (MessageBox.Show(@"是否确认退出程序？", @"退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) {
                if (systemLogs.Count > 0) {
                    ShowWaitForm();
                    await _logInfoService.Insert(systemLogs);
                    HideWaitForm();
                }
                // 关闭所有的线程
                this.Dispose();
                this.Close();
            } else {
                e.Cancel = true;
            }
        }
        /// <summary>
        /// 窗口加载时执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartForm_Load(object sender, EventArgs e) {
            CheckForIllegalCrossThreadCalls = false;
            InitializeServerCard();
        }
        /// <summary>
        /// 每当窗口第一次加载完成时执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Shown(object sender, EventArgs e) {
            InitCheck();
            //订阅消息
            var evt = new EventBusRabbitMQ(_mQConnection);
            evt.Subscribe("log", SaveLogInfo);
            logHandler.WriteLog("信息", "日志监听程序启动");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        private async void SaveLogInfo(string msg) {
            var t = JsonConvert.DeserializeObject<SystemLogModel>(msg);
            if (t.Id.IsEmpty()) {
                t.Id = Guid.NewGuid().ToString();
            }
            systemLogs.Add(t);
            int res = 0;
            if (systemLogs.Count >= 10) {
                res = await _logInfoService.Insert(systemLogs);
            }
            if (res > 0) {
                systemLogs.Clear();
            }
        }
        #endregion



    }

}
